import 'package:flutter/material.dart';
import 'package:super_tool_server/component/animation/loading_animate.dart';
import 'package:super_tool_server/component/clickable/clickable_widget.dart';
import 'package:super_tool_server/component/theme/theme.dart';

enum RoundButtonStyle {
  normal, // 正常，主色底，白色字
  light, // 浅色按钮，用在深色背景， 白色背景，主色文字
  gray, // 灰色按钮，用在浅色背景， 灰色背景，黑色文字
  ghost, // 幽灵按钮， 主色字和边框， 透明背景
  operation, // 操作按钮， 用在操作按钮， 白色背景，灰色边框，黑色文字
  text, // 文本黑色，背景透明，无边框
  custom, // 自定义， 仅在此模式背景色和文字颜色有效， 默认值和 normal 相同
  weak, // 弱按钮， 背景是浅紫色
}

enum RoundButtonShape {
  circle, // 圆形按钮
  round, // 方形按钮
}

enum RoundButtonSize {
  large, // 大  高度 手机 52  PC 50
  normal, // 正常  高度 手机 44  PC 38
  small, // 小  高度 手机 32 PC 30
  extraSmall, // 非常小  高度28
}

/// 圆角按钮
class RoundButton extends StatelessWidget {
  RoundButton({
    this.uuid,
    Key? key,
    this.style = RoundButtonStyle.normal,
    this.shape = RoundButtonShape.circle,
    this.size = RoundButtonSize.normal,
    this.mainAxisSize = MainAxisSize.max,
    this.iconScale = 1.0,
    this.text,
    this.fontSize,
    this.fontWeight = FontWeight.normal,
    this.leftIconData,
    this.rightIconData,
    this.onPress,
    this.isLoading = false,
    this.loadingAnimationStyle,
    this.isEnable = true,
    this.isSelected = false,
    this.isDanger = false,
    this.useMinWidth = true,
    this.textColor,
    this.backgroundColor,
    this.gradient,
    this.borderColor,
    this.borderRadius,
    this.border,
    this.name,
    this.parameter,
    this.contentWidget,
  }) : super(key: key);

  /// uuid
  final String? uuid;

  /// 按钮样式
  final RoundButtonStyle style;

  /// 按钮形状
  final RoundButtonShape shape;

  /// 按钮大小
  final RoundButtonSize size;

  /// 按钮大小是否扩展
  final MainAxisSize mainAxisSize;

  /// 显示文本
  final String? text;

  ///左侧的icon
  final Widget? leftIconData;

  ///右侧的icon
  final Widget? rightIconData;

  /// 点击回调
  final VoidCallback? onPress;

  /// 是否显示菊花
  final bool isLoading;

  final LoadingAnimationStyle? loadingAnimationStyle;

  /// 是否是可以用状态
  final bool isEnable;

  /// 是否是选中态
  final bool isSelected;

  /// 是否是危险按钮
  final bool isDanger;

  /// 文字颜色（仅在 style 为 custom 时有效）
  final Color? textColor;

  /// 背景颜色（仅在 style 为 custom 时有效）
  final Color? backgroundColor;

  ///  背景颜色-渐变色
  final LinearGradient? gradient;

  /// 边框颜色（仅在 style 为 custom 时有效）
  final Color? borderColor;

  /// 圆角
  final double? borderRadius;

  final Border? border;

  /// icon 缩放比例
  final double iconScale;
  final double? fontSize;
  final FontWeight? fontWeight;

  /// 内容 Widget，设置了此参数，其他的几个参数的显示状态均失效
  final Widget? contentWidget;

  /// 过渡使用, 默认限制按钮宽度为最小宽度
  final bool useMinWidth;

  /// 上报名称
  final String? name;

  /// 上报参数
  final Map<String, dynamic>? parameter;

  Color _getPrimaryColor(BuildContext context) {
    Color color = CustomTheme.of(context)!.colors.primary;
    if (isDanger) {
      color = CustomTheme.of(context)!.colors.danger;
    }
    if (isSelected) {
      color = CustomTheme.of(context)!.colors.background.gray20;
    }
    return color;
  }

  Color _backgroundColor(BuildContext context) {
    if (style == RoundButtonStyle.normal) {
      if (isEnable) {
        return _getPrimaryColor(context);
      } else {
        return CustomTheme.of(context)!.colors.background.primaryDisabled;
      }
    }
    if (style == RoundButtonStyle.light ||
        style == RoundButtonStyle.operation) {
      return Colors.white;
    }
    if (style == RoundButtonStyle.gray) {
      return CustomTheme.of(context)!.colors.background.gray5;
    }
    if (style == RoundButtonStyle.ghost) {
      return Colors.white;
    }

    if (style == RoundButtonStyle.text) {
      return Colors.transparent;
    }

    if (style == RoundButtonStyle.weak) {
      return CustomTheme.of(context)!.colors.background.primary20;
    }

    if (style == RoundButtonStyle.custom) {
      return backgroundColor ?? _getPrimaryColor(context);
    }

    return _getPrimaryColor(context);
  }

  Color _defaultTextColor(BuildContext context) {
    if (style == RoundButtonStyle.normal) {
      if (isEnable) {
        return Colors.white;
      } else {
        return Colors.white.withOpacity(0.5);
      }
    }

    if (style == RoundButtonStyle.light || style == RoundButtonStyle.ghost) {
      return _getPrimaryColor(context);
    }
    if (style == RoundButtonStyle.gray) {
      return CustomTheme.of(context)!.colors.text.gray80;
    }
    if (style == RoundButtonStyle.operation || style == RoundButtonStyle.text) {
      if (isDanger) {
        return CustomTheme.of(context)!.colors.danger;
      }
      return CustomTheme.of(context)!.colors.text.dark;
    }

    if (style == RoundButtonStyle.weak) {
      return CustomTheme.of(context)!.colors.primary;
    }

    if (style == RoundButtonStyle.custom) {
      return textColor ?? Colors.white;
    }

    return Colors.white;
  }

  double _defaultBorderRadius(BuildContext context) {
    if (shape == RoundButtonShape.circle) {
      return 99.0;
    } else if (shape == RoundButtonShape.round) {
      return 10.0;
    }

    return 0.0;
  }

  double _defaultHeight(BuildContext context) {
    if (shape == RoundButtonShape.circle) {
      if (size == RoundButtonSize.large) {
        return 52.0;
      }
      if (size == RoundButtonSize.normal) {
        return 44.0;
      }
      if (size == RoundButtonSize.small) {
        return 32;
      }
      if (size == RoundButtonSize.extraSmall) {
        return 28;
      }
    }
    if (shape == RoundButtonShape.round) {
      if (size == RoundButtonSize.large) {
        return 50;
      }
      if (size == RoundButtonSize.normal) {
        return 38;
      }
      if (size == RoundButtonSize.small) {
        return 30;
      }
      if (size == RoundButtonSize.extraSmall) {
        return 28;
      }
    }

    return 0.0;
  }

  double _defaultFontSize(BuildContext context) {
    if (style == RoundButtonStyle.custom && fontSize != null) {
      return fontSize!;
    }
    if (shape == RoundButtonShape.circle) {
      if (size == RoundButtonSize.large) {
        return 18;
      }
      if (size == RoundButtonSize.normal) {
        return 16;
      }
      if (size == RoundButtonSize.small) {
        return 14;
      }
      if (size == RoundButtonSize.extraSmall) {
        return 12;
      }
    }
    if (shape == RoundButtonShape.round) {
      if (size == RoundButtonSize.large) {
        return 16;
      }
      if (size == RoundButtonSize.normal) {
        return 14;
      }
      if (size == RoundButtonSize.small) {
        return 12;
      }
      if (size == RoundButtonSize.extraSmall) {
        return 12;
      }
    }

    return 0.0;
  }

  Border? _defaultBorder(BuildContext context) {
    if (style == RoundButtonStyle.ghost) {
      return Border.all(color: _getPrimaryColor(context), width: 1);
    }

    if (style == RoundButtonStyle.operation) {
      return Border.all(
          color: CustomTheme.of(context)!.colors.border.gray10, width: 1);
    }

    if (style == RoundButtonStyle.custom && borderColor != null) {
      return Border.all(color: borderColor!, width: 2);
    }

    return null;
  }

  double _defaultOpacity(BuildContext context, bool isHighlight) {
    return 1.0;
  }

  double _defaultContentOpacity(BuildContext context, bool isHighlight) {
    if (!isEnable) {
      return 0.3;
    }
    return 1.0;
  }

  double _defaultHorizontalPadding(BuildContext context) {
    if (style == RoundButtonStyle.custom) {
      return 0;
    }
    if (shape == RoundButtonShape.circle) {
      if (size == RoundButtonSize.large) {
        return 20;
      }
      if (size == RoundButtonSize.normal) {
        return 15;
      }
      if (size == RoundButtonSize.small) {
        return 15;
      }
      if (size == RoundButtonSize.extraSmall) {
        return 10;
      }
    }
    if (shape == RoundButtonShape.round) {
      if (size == RoundButtonSize.large) {
        return 45;
      }
      if (size == RoundButtonSize.normal) {
        return 20;
      }
      if (size == RoundButtonSize.small) {
        return 15;
      }
      if (size == RoundButtonSize.extraSmall) {
        return 10;
      }
    }
    return 0.0;
  }

  double _defaultMinWidth(BuildContext context) {
    if (shape == RoundButtonShape.round) {
      if (size == RoundButtonSize.large) {
        return 240;
      }
      if (size == RoundButtonSize.normal) {
        return 100;
      }
      if (size == RoundButtonSize.small) {
        return 58;
      }
      if (size == RoundButtonSize.extraSmall) {
        return 48;
      }
    }
    return 0.0;
  }

  List<BoxShadow> _defaultShadow(BuildContext context) {
    if (style == RoundButtonStyle.light && shape == RoundButtonShape.circle) {
      return [
        BoxShadow(
            color: CustomTheme.of(context)!.colors.shadow.box,
            offset: Offset(0.0, 4.0),
            blurRadius: 10.0),
      ];
    }

    return [];
  }

  LoadingAnimationStyle _loadingAnimationStyle(BuildContext context) {
    if (style == RoundButtonStyle.ghost) {
      return LoadingAnimationStyle.circleAllPrimary;
    }
    return LoadingAnimationStyle.circleAllWhite;
  }

  @override
  Widget build(BuildContext context) {
    return ClickableWidget(
      onTap: isEnable && !isLoading
          ? (onPress == null
              ? null
              : () {
                  onPress!();
                })
          : null,
      builder: (BuildContext context, ClickableBuilderInfo info) {
        if (!useMinWidth) {
          return Opacity(
            opacity: _defaultOpacity(context, info.isHighlight),
            child: Container(
              constraints: BoxConstraints(
                minWidth: _defaultMinWidth(context),
              ),
              height: _defaultHeight(context),
              decoration: BoxDecoration(
                color: gradient != null ? null : _backgroundColor(context),
                borderRadius: BorderRadius.all(Radius.circular(
                    borderRadius ?? _defaultBorderRadius(context))),
                border: border ?? _defaultBorder(context),
                boxShadow: _defaultShadow(context),
                gradient: gradient,
              ),
              child: Opacity(
                opacity: _defaultContentOpacity(context, info.isHighlight),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Expanded(child: Container()),
                    contentWidget ??
                        Row(
                          mainAxisAlignment: MainAxisAlignment.center,
                          mainAxisSize: mainAxisSize,
                          children: <Widget>[
                            if (leftIconData != null) leftIconData!,
                            if (isLoading)
                              Padding(
                                padding: EdgeInsets.only(right: 5),
                                child: LoadingAnimationWidget(
                                  style: loadingAnimationStyle ??
                                      _loadingAnimationStyle(context),
                                  size: _defaultFontSize(context) * 1.25,
                                ),
                              ),
                            Text(
                              text!,
                              style: TextStyle(
                                  fontSize: _defaultFontSize(context),
                                  fontWeight: fontWeight,
                                  color: _defaultTextColor(context)),
                            ),
                            if (rightIconData != null) rightIconData!,
                          ],
                        ),
                    Expanded(child: Container()),
                  ],
                ),
              ),
            ),
          );
        }
        return Opacity(
          opacity: _defaultOpacity(context, info.isHighlight),
          child: Container(
            constraints: BoxConstraints(
              minWidth: _defaultMinWidth(context),
            ),
            height: _defaultHeight(context),
            decoration: BoxDecoration(
              color: info.isHover && isEnable
                  ? _backgroundColor(context).withOpacity(0.8)
                  : (gradient != null ? null : _backgroundColor(context)),
              borderRadius: BorderRadius.all(Radius.circular(
                  borderRadius ?? _defaultBorderRadius(context))),
              border: border ?? _defaultBorder(context),
              boxShadow: _defaultShadow(context),
              gradient: info.isHover && isEnable ? null : gradient,
            ),
            child: Opacity(
              opacity: _defaultContentOpacity(context, info.isHighlight),
              child: Padding(
                padding: EdgeInsets.symmetric(
                    horizontal: _defaultHorizontalPadding(context)),
                child: contentWidget ??
                    Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      mainAxisSize: mainAxisSize,
                      children: <Widget>[
                        if (leftIconData != null) leftIconData!,
                        if (isLoading)
                          Padding(
                            padding: EdgeInsets.only(right: 5),
                            child: LoadingAnimationWidget(
                              style: loadingAnimationStyle ??
                                  _loadingAnimationStyle(context),
                              size: _defaultFontSize(context) * 1.25,
                            ),
                          ),
                        Text(
                          text!,
                          style: TextStyle(
                              fontSize: _defaultFontSize(context),
                              fontWeight: fontWeight,
                              color: info.isHover
                                  ? _defaultTextColor(context)
                                  : _defaultTextColor(context)),
                        ),
                        if (rightIconData != null) rightIconData!,
                      ],
                    ),
              ),
            ),
          ),
        );
      },
    );
  }
}
